import time
from tkinter.tix import Select
from selenium.webdriver.support.ui import WebDriverWait
from Lian8_261.confirm import alerrt
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

class Base():
    def __init__(self,driver):
        self.driver = driver

    def open_url(self, url):
        self.driver.get(url)

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
            print()

    def click(self, url):
        self.find_element(url).click()

    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)

    def text(self, url):
        return self.find_element(url).text

        # 滚动

    def scroll_x(self, x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    #滑动到底部
    def scrollTo_height(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        #封装一个截图方法系统时间的

    def gettime(self):
        return time.strftime('%Y%m%d%H%M%S',time.localtime())

    def tupian(self):
        self.driver.save_screenshot(self.gettime()+'.png')

    #封装alert事件-accept()
    def alert_accept(self):
        alert = self.driver.switch_to.alert
        alert.accept()

    #封装confirm_dismiss()
    def confirm_dismiss(self):
        confirm = self.driver.switch_to.alert
        confirm.dismiss()

    #封装prompt事件-accept()
    def prompt_accept(self):
        prompt = self.driver.switch_to.alert
        prompt.send_keys('这是一个prompt弹出')
        prompt.accept()
    #封装prompt事件-dismiss()
    def prompt_dismiss(self):
        prompt = self.driver.switch_to.alert
        prompt.send_keys('这是一个prompt弹出')
        prompt.dismiss()
    #封装窗口切换
    def window_handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #封装进入frame框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    #封装退出frame框架
    def frame_connect(self):
        self.driver.switch_to.default_content()

    #封装select-index
    def select_index(self,loc,x):
        index = Select(self.find_element(loc))
        index.select_by_index(x)

    #封装select-value
    def select_value(self,loc,x):
        value = Select(self.find_element(loc))
        value.select_by_value(x)

    #封装select-value-text
    def select_text(self, loc, info):
        text = Select(self.find_element(loc))
        text.select_by_visible_text(info)

    #鼠标的封装
    #封装鼠标双击
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #封装鼠标三击
    def three_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click(self.find_element(loc)).perform()
    #封装鼠标右击
    def contest_click(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()
    # 封装鼠标悬停
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    # 封装鼠标拖拽
    def drag_and_drop(self,loc,x,y):
        ActionChains(self.driver).drag_and_drop_by_offset(self.find_element(loc),x,y).perform()
    #封装鼠标按住不放
    def click_and_hold(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()
    #键盘的封装
    def keys_enter(self,loc):
        self.send_keys(loc,Keys.ENTER)
        #键盘TAB
    def keys_tab(self,loc):
        self.send_keys(loc,Keys.TAB)
        # 键盘DELETE

    def keys_DELETE(self, loc):
        self.send_keys(loc, Keys.DELETE)
        # 键盘TAB

    def keys_tab(self, loc):
        self.send_keys(loc, Keys.TAB)

        # 键盘TAB

    def keys_tab(self, loc):
        self.send_keys(loc, Keys.TAB)